package com.iot.inter.controller.newinterface.extra;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.OrderPaymentManager;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.extra.extrapackages.request.RequestExtraPackagesSync;
import com.iot.inter.entity.newrequest.extra.extrapackages.response.ResponseExtraPackagesSync;
import com.iot.inter.interfaceprocessing.newinterface.TurnToExtraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.19获取加油包流量信息:2.0接口
 * <p>
 * 接入方可以通过调用该接口，可以获取允许订购的加油包流量信息；
 *
 * @author lm
 * @date 2019/8/5
 */
@RestController
@Api(description = "3.19获取加油包流量信息")
public class ExtraPackagesSyncController {
    private static Logger logger = Logger.getLogger(ExtraPackagesSyncController.class);

    @CrossOrigin
    @RequestMapping(value = "extraSync", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.19获取加油包流量信息", response = ResponseExtraPackagesSync.class)
    public String extraSync(@ApiParam @RequestBody RequestExtraPackagesSync request) {

        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);

        if (requestStr != null) {

            logger.info("【获取加油包流量信息】请求：" + requestStr);

            RequestExtraPackagesSync requestPackage = JSONArray.parseObject(requestStr, RequestExtraPackagesSync.class);
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            //3.19	获取加油包流量信息
            TurnToExtraService extra = new TurnToExtraService();
            OrderPaymentManager response = extra.extraSync(secret, partnerCode);

            if (response != null) {

                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(response.getRetCode())) {

                    ResponseExtraPackagesSync res = new ResponseExtraPackagesSync();
                    res.setFlowList(response.getFlowList());
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【获取加油包流量信息】成功响应：" + jsonString);
                    return jsonString;

                } else {

                    ResponseExtraPackagesSync res = new ResponseExtraPackagesSync();
                    res.setRetCode(response.getRetCode());
                    res.setResultInfo(response.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(res);
                    logger.info("【获取加油包流量信息】响应：" + jsonString);
                    return jsonString;

                }


            } else {

                ResponseExtraPackagesSync res = new ResponseExtraPackagesSync();
                res.setRetCode("9998");
                res.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(res);
                logger.info("【获取加油包流量信息】响应：" + jsonString);
                return jsonString;

            }

        } else {
            ResponseExtraPackagesSync res = new ResponseExtraPackagesSync();
            res.setRetCode("9998");
            res.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(res);
            logger.info("【获取加油包流量信息】响应：" + jsonString);
            return jsonString;

        }
    }

}
